/********************************************************************************
 * BannerAdvertisersAdminAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Dec 2, 2008 4:47:53 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.BannerFees;
import com.printeverywhere.dto.BannerSettings;
import com.printeverywhere.util.SearchCriteria;

public class BannerSettingsAdminAction extends AdminEngineManagement implements ServletRequestAware {

	/** */
	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;

	private List<BannerSettings> bannerSettingsList = new ArrayList<BannerSettings>();

	private BannerSettings bannerSettings;

	private BannerFees bannerFees = new BannerFees();

	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		this.bannerSettingsList = (List<BannerSettings>) CommonFunctionDelegator.getAllfromtable(BannerSettings.class.getName());
		return SUCCESS;
	}

	public String addBannerSettings() throws Exception {

		Pattern patternForNo = null;
		Pattern patternForDimension = null;
		patternForNo = Pattern.compile("^[0-9]*");
		patternForDimension = Pattern.compile("^[<>=0-9]*");

				if(request.getParameter("bannerSettings.formatName").equals("")){
					addActionError("Insert banner format name");
				}else if (request.getParameter("bannerSettings.formatName").trim().length() > 50) {
					addActionError("Banner format name length less than 50 Character");
				}
				if(request.getParameter("bannerSettings.noOfFormat").equalsIgnoreCase("")){
					addActionError("Insert number of banners ");
				}else{
					Matcher fit = patternForNo.matcher(request.getParameter("bannerSettings.noOfFormat").trim());
					if(!fit.matches()){
						addActionError("Number of banners allows Positive numbers only");
					}
				}
				if(request.getParameter("bannerSettings.maxWidth").equalsIgnoreCase("")){
					addActionError("Insert banner format maxWidth");
				}else{
					Matcher fitDimension = patternForNo.matcher(request.getParameter("bannerSettings.maxWidth"));
					if(!fitDimension.matches()){
						addActionError("Banner format maxWidth allow 0-9");
					}
				}
				if(request.getParameter("bannerSettings.maxHieght").equalsIgnoreCase("")){
					addActionError("Insert banner format maxHieght");
				}else{
					Matcher fitDimension = patternForNo.matcher(request.getParameter("bannerSettings.maxHieght"));
					if(!fitDimension.matches()){
						addActionError("Banner format maxHieght allow 0-9");
					}
				}

				if(request.getParameter("bannerSettings.baseHieghtWidth").equalsIgnoreCase("")){
					addActionError("Insert banner format baseHieghtWidth");
				}else{
					Matcher fitDimension = patternForNo.matcher(request.getParameter("bannerSettings.baseHieghtWidth"));
					if(!fitDimension.matches()){
						addActionError("Banner format baseHieghtWidth allow <,>,= ,[0-9]");
					}
				}
				if(hasActionErrors()){
					return INPUT;
				}
			try{
				Integer formatNo = bannerSettings.getNoOfFormat();
				SearchCriteria searchCriteria = new SearchCriteria();
				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute("formatName", request.getParameter("bannerSettings.formatName"));
				List<BannerSettings> list = (List<BannerSettings>) CommonFunctionDelegator.GeneralgetCriteriaList(BannerSettings.class.getName() , searchCriteria);
				if(list.size() > 0){
					addActionError("Banner format with this name is already exist please try another");
					return INPUT;
				}
				CommonFunctionDelegator.generalInsertbyobj(this.bannerSettings);
				this.bannerFees.setBannerSettings(this.bannerSettings);
				this.bannerFees.setFeesPerClick(0.0);
				this.bannerFees.setFeesPerDay(0.0);
				this.bannerFees.setFeesPerView(0.0);
				CommonFunctionDelegator.generalInsertbyobj(this.bannerFees);
			}catch (Exception e) {
				addActionError("Error while inserting banner format");
				e.printStackTrace();
				return INPUT;
			}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String editBannerSettings() throws Exception {
		if(request.getParameter("settingId") != null) {
			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute("id", Integer.parseInt(request.getParameter("settingId")));
			bannerSettingsList = (List<BannerSettings>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannerSettings.class);
			if(bannerSettingsList.size() > 0) {
				bannerSettings = bannerSettingsList.get(0);
			} else {
				bannerSettings = new BannerSettings();
			}
		} else {
			bannerSettings = new BannerSettings();
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String toggleBannerSettings() throws Exception {
		if(request.getParameter("settingId") != null) {
			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute("id", Integer.parseInt(request.getParameter("settingId")));
			bannerSettingsList = (List<BannerSettings>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannerSettings.class);
			if(bannerSettingsList.size() > 0) {
				this.bannerSettings = bannerSettingsList.get(0);
				this.bannerSettings.setEnable(!this.bannerSettings.isEnable());
				CommonFunctionDelegator.generalInsertbyobj(this.bannerSettings);
			}
		}
		return SUCCESS;
	}

	public String deleteBannerSettings() throws Exception {
		try{
			if(request.getParameter("id") != null) {
				SearchCriteria searchCriteria = new SearchCriteria();
				searchCriteria.setAttribute("id", Integer.parseInt(request.getParameter("id")));
				bannerSettingsList = (List<BannerSettings>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannerSettings.class);
				if(bannerSettingsList.size() > 0) {
					bannerSettings = bannerSettingsList.get(0);
					CommonFunctionDelegator.GeneralDelete(bannerSettings);
					this.bannerSettingsList = (List<BannerSettings>) CommonFunctionDelegator.getAllfromtable(BannerSettings.class.getName());
					addActionMessage("Selected banner setting Delete Successfully");
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			this.bannerSettingsList = (List<BannerSettings>) CommonFunctionDelegator.getAllfromtable(BannerSettings.class.getName());
			addActionError("Error while deleting banner settings");
			return INPUT;
		}

		return SUCCESS;
	}

	public List<BannerSettings> getBannerSettingsList() {
		return bannerSettingsList;
	}

	public BannerSettings getBannerSettings() {
		return bannerSettings;
	}

	public void setBannerSettings(BannerSettings bannerSettings) {
		this.bannerSettings = bannerSettings;
	}

//	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public BannerFees getBannerFees() {
		return bannerFees;
	}

	public void setBannerFees(BannerFees bannerFees) {
		this.bannerFees = bannerFees;
	}
}
